package com.fuyu.system.action;

import com.fuyu.system.anno.IgnoreWebSecurity;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

public interface ApiIgnore {

    String[] getIgnore();

    Set<String> getIgnorePathSet();


    default String[] getIgnoreApi(RequestMappingHandlerMapping requestMappingHandlerMapping) {
        //找到所以标记忽略认证注解的api
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
            HandlerMethod handlerMethod = entry.getValue();
            if (handlerMethod.hasMethodAnnotation(IgnoreWebSecurity.class)) {
                this.getIgnorePathSet().addAll(entry.getKey().getPatternValues());
            }
        }
        //合并标记的api路径ignoreWeb里的路径
        this.getIgnorePathSet().addAll(Arrays.asList(this.getIgnore()));
        return this.getIgnorePathSet().toArray(new String[0]);
    }
}
